<template>
  <div class="region">
    <div class="content">
      <div class="left">地区：</div>
      <ul>
        <li :class="{ active: regionFlag == '' }" @click="changeRegion('')">全部</li>
        <li v-for="region in regionArr" :key="region.value" :class="{ active: regionFlag == region.value }"
          @click="changeRegion(region.value)">{{
            region.name }}</li>
      </ul>
    </div>
  </div>
</template>

<script setup lang="ts" name="Region">
//@ts-ignore
import { reqHospitalLevelAndRegion } from '@/api/home/index'
//@ts-ignore
import type { HospitalLevelAndRegionResponseData, HospitalLevelAndRegionArr } from '@/api/home/type'
import { onMounted, ref } from 'vue';

//存储地区数据的数组
let regionArr = ref<HospitalLevelAndRegionArr>([])
//控制地区高亮的响应式数据
let regionFlag = ref<string>('')

//获取地区数据
const getRegion = async () => {
  let result: HospitalLevelAndRegionResponseData = await reqHospitalLevelAndRegion('Beijin')
  if (result.code == 200) {
    regionArr.value = result.data
  }
}
//组件挂载完毕：发送请求
onMounted(() => {
  getRegion()
})
//点击不同地区高亮的回调：地区高亮，（声明）触发自定义事件
//声明自定义事件
let $emit = defineEmits(['getRegion'])
const changeRegion = (region: string) => {
  regionFlag.value = region
  //触发自定义事件：将地区参数回传给父组件
  $emit('getRegion', region)
}
</script>

<style scoped lang="scss">
.region {
  color: #7f7f7f;
  margin-top: 10px;

  .content {
    display: flex;

    .left {
      margin-right: 10px;
      width: 60px;
    }

    ul {
      display: flex;
      flex-wrap: wrap;

      li {
        margin-right: 10px;
        margin-bottom: 10px;

        &.active {
          color: #55a6fe;
        }
      }

      li:hover {
        color: #55a6fe;
        cursor: pointer;
      }
    }
  }
}
</style>